var Player = cc.Class({
  extends: cc.Component,

  properties: () => ({
    prefabManager: {
      default: null,
      type: require("PrefabManager")
    },
    game: {
      default: null,
      type: require("Game")
    },
    colliderjs: {
      default: null,
      type: require("ColliderRange")
    },
    colliderRangeNode: {
      default: null,
      type: cc.Node
    },
    bitBAudio: {
      default: null,
      type: cc.AudioClip
    },
    bitWAudio: {
      default: null,
      type: cc.AudioClip
    },
    hitAudio: {
      default: null,
      type: cc.AudioClip
    },
    beHitAnm: {
      default: null,
      type: cc.Animation
    },
  }),
  // 初始化 获取龙骨动画名称
  onLoad: function() {
    this.comboWhite = 0;
    this.comboBlack = 0;
    this.delayNum = 0;
    this.totalDelayNum = 3.6;
    this.delayBatNum = 0;
    this.totalDelayBatNum = 4;
    this._armatureDisPlay = this.getComponent(dragonBones.ArmatureDisplay);
    this._armature = this._armatureDisPlay.armature();
  },
  update: function(dt) {
    if (this._armatureDisPlay.animationName == 'skill' && this.game.isRuning) {
      this.delayNum += dt;
      this.game.skillCD.getComponent(cc.ProgressBar).progress = 1 - (this.delayNum / this.totalDelayNum);
      return;
    }else if (this._armatureDisPlay.animationName == 's2begin' && this.game.isRuning) {
      this.delayBatNum += dt;
      this.game.batiCD.getComponent(cc.ProgressBar).progress = 1 - (this.delayBatNum / this.totalDelayBatNum);
      return;
    }else if (this._armatureDisPlay.animationName == 's2loop' && this.game.isRuning) {
      this.delayBatNum += dt;
      this.game.batiCD.getComponent(cc.ProgressBar).progress = 1 - (this.delayBatNum / this.totalDelayBatNum);
      return;
    }else if (this._armatureDisPlay.animationName == 's2end' && this.game.isRuning) {
      this.delayBatNum += dt;
      this.game.batiCD.getComponent(cc.ProgressBar).progress = 1 - (this.delayBatNum / this.totalDelayBatNum);
      return;
    } else if (this._armatureDisPlay.animationName == 'run') {
      this.delayNum = 0;
      this.delayBatNum = 0;
      return;
    }
  },
  // 主角开始移动
  onPlayerStartMove: function() {
    if (!this.game.isRuning) return;
    this._armatureDisPlay.playAnimation('run', 0);
  },
  // 主角死亡
  onPlayerStopMove: function() {
    this._armatureDisPlay.removeEventListener(dragonBones.EventObject.COMPLETE, this.backRun, this);
    this._armatureDisPlay.playAnimation('die', 1);
  },
  // 主角动画暂停
  onPlayerPause: function() {
    this._armatureDisPlay.armature().animation.stop();
  },
  // 主角动画继续
  onPlayerResume: function() {
    this._armatureDisPlay.armature().animation.play();
  },
  // 主角复活动画
  onPlayerReLive: function() {
    this._armatureDisPlay.playAnimation('relive', 1);
  },
  // 碰撞检测
  onCollisionEnter: function(other, self) {
    if (!this.game.isRuning) return;
    if (other.tag == 1001 || other.tag == 1002) {
      cc.audioEngine.play(this.hitAudio);
      //怪物靠近时播放攻击动画
      other.getComponent(dragonBones.ArmatureDisplay).playAnimation('attack', 1);
      //攻击结束后继续奔跑
      this.scheduleOnce(function() {
        other.getComponent(dragonBones.ArmatureDisplay).playAnimation('run', 0);
      }, 0.3);

      this._armatureDisPlay.playAnimation('hurt', 1);
      this.beHitAnm.play('bHit'); //受击特效
      this.game.deductBlood(other.node);
      if (this.game.bloodVolume != 0) {
        this._armatureDisPlay.addEventListener(dragonBones.EventObject.COMPLETE, this.backRun, this);
      }
    };
  },
  // 白键按下后跳跃动作
  jumpWhite: function() {
    if (this.comboWhite == 0) {
      // this.game._isLeft = false;
      cc.audioEngine.play(this.bitWAudio);
      this._armatureDisPlay.playAnimation('atkW01', 1);
      this._armatureDisPlay.addEventListener(dragonBones.EventObject.COMPLETE, this.backRun, this);
      this.comboWhite = 1;
      this.comboBlack = 0;
    } else if (this.comboWhite == 1) {
      // this.game._isLeft = false;
      cc.audioEngine.play(this.bitWAudio);
      this._armatureDisPlay.playAnimation('atkW02', 1);
      this._armatureDisPlay.addEventListener(dragonBones.EventObject.COMPLETE, this.backRun, this);
      this.comboWhite = 2;
      this.comboBlack = 0;
    } else if (this.comboWhite == 2) {
      // this.game._isLeft = false;
      cc.audioEngine.play(this.bitWAudio);
      this._armatureDisPlay.playAnimation('atkW03', 1);
      this._armatureDisPlay.addEventListener(dragonBones.EventObject.COMPLETE, this.backRun, this);
      this.comboWhite = 0;
      this.comboBlack = 0;
    }
    this.comboCount();
  },
  // 黑键按下后跳跃动作
  jumpBlack: function() {
    if (this.comboBlack == 0) {
      // this.game._isRight = false;
      cc.audioEngine.play(this.bitBAudio);
      this._armatureDisPlay.playAnimation('atkB01', 1);
      this._armatureDisPlay.addEventListener(dragonBones.EventObject.COMPLETE, this.backRun, this);
      this.comboBlack = 1;
      this.comboWhite = 0;
    } else if (this.comboBlack == 1) {
      // this.game._isRight = false;
      cc.audioEngine.play(this.bitBAudio);
      this._armatureDisPlay.playAnimation('atkB02', 1);
      this._armatureDisPlay.addEventListener(dragonBones.EventObject.COMPLETE, this.backRun, this);
      this.comboBlack = 2;
      this.comboWhite = 0;
    } else if (this.comboBlack == 2) {
      // this.game._isRight = false;
      cc.audioEngine.play(this.bitBAudio);
      this._armatureDisPlay.playAnimation('atkB03', 1);
      this._armatureDisPlay.addEventListener(dragonBones.EventObject.COMPLETE, this.backRun, this);
      this.comboBlack = 0;
      this.comboWhite = 0;
    }
    this.comboCount();
  },
  // 是否空斩
  comboCount: function() {
    if(this.colliderjs.isTouch) {
      this.colliderjs.isTouch = false;
    }else if(!this.colliderjs.isTouch) {
      this.game.comboNum = 0;
    }
  },
  // 必杀技按下后动作
  jumpSkill: function() {
    this.game.skillCD.active = true;
    this.game._totalNum -= this.game.zhengqi;
    this.game.skillCD.getComponent(cc.ProgressBar).progress = 1;
    this.game.skillBtn.getComponent(cc.Button).enabled = false;
    this.game._isSkill = true;
    this.game._skillShow = false;
    this.game._isLeft = false;
    this.game._isRight = false;
    cc.audioEngine.play(this.bitBAudio);
    this._armatureDisPlay.playAnimation('skill', 2);
    this.colliderRangeNode.getComponent(cc.BoxCollider).size.width = 700;
    this._armatureDisPlay.addEventListener(dragonBones.EventObject.COMPLETE, this.backRun, this);
  },
  // 返回奔跑状态
  backRun: function() {
    this.game.skillCD.active = false;
    this.game.skillCD.getComponent(cc.ProgressBar).progress = 0;
    this.game.skillBtn.getComponent(cc.Button).enabled = true;
    this._armatureDisPlay.playAnimation('run', 0);
    // this.game._isLeft = true;
    // this.game._isRight = true;
    this.game._isSkill = false;
    this.colliderRangeNode.getComponent(cc.BoxCollider).size.width = 150;
    this._armatureDisPlay.removeEventListener(dragonBones.EventObject.COMPLETE, this.backRun, this);
  },
  // 霸体技能按下后动作
  jumpBati: function() {
    this.game._isBati = true;
    this.game._isLeft = false;
    this.game._isRight = false;
    this.game.batiCD.active = true;
    this.game.batiCD.getComponent(cc.ProgressBar).progress = 1;
    this.game.batiBar.getChildByName('BatiBtn').getComponent(cc.Button).enabled = false;
    this._armatureDisPlay.playAnimation('s2begin', 1);
    this.colliderRangeNode.getComponent(cc.BoxCollider).size.width = 200;
    this._armatureDisPlay.addEventListener(dragonBones.EventObject.COMPLETE, this.bati2, this);
  },
  bati2: function() {
    this._armatureDisPlay.removeEventListener(dragonBones.EventObject.COMPLETE, this.bati2, this);
    this._armatureDisPlay.playAnimation('s2loop', 7);
    this._armatureDisPlay.addEventListener(dragonBones.EventObject.COMPLETE, this.bati3, this);
  },  
  bati3: function() {
    this._armatureDisPlay.removeEventListener(dragonBones.EventObject.COMPLETE, this.bati3, this);
    this._armatureDisPlay.playAnimation('s2end', 1);
    this._armatureDisPlay.addEventListener(dragonBones.EventObject.COMPLETE, this.batibackRun, this);
  },
  batibackRun: function() {
    this.game._isBati = false;
    this.game._isLeft = true;
    this.game._isRight = true;
    this.game.batiCD.active = false;
    this.game.batiCD.getComponent(cc.ProgressBar).progress = 1;
    this._armatureDisPlay.playAnimation('run', 0);
    this.game.batiBar.getChildByName('BatiBtn').getComponent(cc.Button).enabled = true;
    this.colliderRangeNode.getComponent(cc.BoxCollider).size.width = 150;
    this._armatureDisPlay.removeEventListener(dragonBones.EventObject.COMPLETE, this.batibackRun, this);
  }
});
module.exports = Player;